﻿//======================================================================
//
//        Copyright (C) 苏州逸仙软件    
//        All rights reserved
//
//        filename :Magazine.aspx.cs
//        description :杂志列表页面
//
//        created by 邹兴武 at  2010-12-30
//
//======================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace huaguwang.hgmanage.Magazine
{
    public partial class Magazine : System.Web.UI.Page
    {
        private readonly D_T_Magazine_Exp _dal = new D_T_Magazine_Exp();
        protected void Page_Load(object sender, EventArgs e)
        {
            //用来初始化用户的按钮,执行此语句的时候,but并没有具体产生
            WebUserToolBar1.MainInit(
              UserConst.Exp_GetUser,
              UserConst.Exp_GetButtonList,
              SetBut,
             UserConst.Exp_ToolBarErr,
              SetText()
           );

            if (!IsPostBack)
            {
                SetData(0);
            }
        }

        private huaguwang.WebControls.WebUserToolBar.SelectItem[] SetText()
        {
            huaguwang.WebControls.WebUserToolBar.SelectItem[] list = { 
                 new huaguwang.WebControls.WebUserToolBar.SelectItem()
              {
                  FuncGetText = () => txtTitle.Text.Trim(),
                  FuncSetText = v => { txtTitle.Text = v; },
                  Name = txtTitle.ID
              },
               new huaguwang.WebControls.WebUserToolBar.SelectItem()
               {
                   FuncGetText = () => txtPTitle.Text.Trim(),
                   FuncSetText = v => { txtPTitle.Text = v; },
                   Name = txtPTitle.ID
               }                                                                     
            };
            return list;
        }

        private void SetBut(int i, Button but)
        {
            switch (i)
            {
                case 1:
                    but.Attributes.Add("onclick", "return GetUpdateCheckDefault();");
                    break;
                case 2:
                    but.Attributes.Add("onclick", "return GetDelCheckDefault();");
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 绑定分页数据
        /// </summary>
        private void SetData(int begin)
        {
            AdminPublic.Try(() =>
            {
                var count = 0;
                var title = this.WebUserToolBar1.MainGetQuery(txtTitle.ID);
                var catetitle = this.WebUserToolBar1.MainGetQuery(txtPTitle.ID);
                this.Repeater1.DataSource = _dal.GetList(begin, Pager.PageSize, title, catetitle, out count);
                this.Repeater1.DataBind();
                this.Pager.RecordCount = count;
            });
        }
        /// <summary>
        /// 分页控件的页面事件
        /// </summary>
        protected void Pager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            SetData((e.NewPageIndex - 1) * Pager.PageSize);
        }

        /// <summary>
        /// 增加按钮事件
        /// </summary>
        protected void WebUserToolBar1_Button0(object sender, EventArgs e)
        {
            var sKey = this.WebUserToolBar1.MainSetSKey();
            Response.Redirect("magazine_e.aspx?" + sKey, false);
        }

        /// <summary>
        /// 修改按钮的事件
        /// </summary>
        protected void WebUserToolBar1_Button1(object sender, EventArgs e)
        {
            var strID = Request.Params["HiddenID"].Exp_Trim();
            var sKey = this.WebUserToolBar1.MainSetSKey();
            Response.Redirect("magazine_e.aspx?ID=" + strID + "&" + sKey, false);
        }

        /// <summary>
        /// 删除按钮的事件
        /// </summary>
        protected void WebUserToolBar1_Button2(object sender, EventArgs e)
        {
            var strID = Request.Params["HiddenID"].Exp_Trim();
            var intA = strID.Exp_SplitToList(',', Sel => Sel.Exp_IntTryParse()).Where(Sel => Sel > 0).ToList();
            if (intA == null || intA.Count <= 0)
            {
                this.Exp_PageAlter("没有找到可以删除的信息");
                return;
            }

            var refInt = _dal.UpdateFlag(intA);
            if (refInt > 0)
            {
                this.Exp_PageWriteLog(Log_Type.delete, "杂志信息", intA);
                this.Exp_PageAlter("删除成功");
                SetData(0);
                return;
            }
            else
            {
                this.Exp_PageAlter("删除操作失败");
                return;
            }
        }

        /// <summary>
        /// 查询按钮事件
        /// </summary>
        protected void WebUserToolBar1_Button3(object sender, EventArgs e)
        {
            WebUserToolBar1.MainSetQuery();
            SetData(0);
        }

    }
}
